package derek.sun;

import static derek.sun.WebChecker.getPageSource;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;

public class Core {
	public static Frame frame;
	public static String reg;

	public static void main(String[] args) throws IOException {
		System.out.println("American Series!");
		frame = new Frame();
		for (int i = 0; i < frame.cmbUsers.getItemCount(); i++) {
			reg = frame.cmbUsers.getItemAt(i).toString();
			// frame.addTreeRootLayer(reg);
			// frame.addTreeAction();
			if (i == 0) {
				char[] s,s1;
				s = reg.toCharArray();
				s1= new char[s.length-1];
				for(int m=1;m<s.length;m++){
					s1[m-1]=s[m];
				}
				getSeriesName(frame, String.valueOf(s1));
			} else {
				getSeriesName(frame, reg.toString().trim());
			}
		}
	}

	public static List<String> getSeriesName(Frame frame, String regstr)
			throws IOException {
		List<String> secondLayer = new ArrayList<String>();
		String url = "http://yyets.com/?mod=2&ac=search_result&op=normal&class=resource&keyword="
				+ regstr.trim();
		String s = getPageSource(url);
		System.out.println(s);
		for (int i = 0; i < frame.tree.firstClassNode.getChildCount(); i++) {
			if (Utils.strComIgnoreCase(frame.tree.firstClassNode.getChildAt(i).toString()
					,regstr)) {
				TreeNode child = frame.tree.firstClassNode.getChildAt(i);
				Pattern pattern = Pattern.compile(
						"<a\\s?href=\"(.{3,50})\">(.*)<font\\scolor=red>("
								+ regstr + ")</font>(.*)<a/>",
						Pattern.CASE_INSENSITIVE);
				String[] split = s.split("</a>");

				DefaultMutableTreeNode nodeZimu = new DefaultMutableTreeNode(
						"字幕");
				frame.tree.addSubNode((DefaultMutableTreeNode) child, nodeZimu);
				for (int j = 0; j < split.length; j++) {
					Matcher matcher = pattern.matcher(split[j]);
					if (matcher.find()) {
						System.out.println(j + ":");
						String tmp = matcher.group(2) + matcher.group(3)
								+ matcher.group(4);
						System.out.println(tmp);
						if (!matcher.group(2).contains("字幕")) {
							DefaultMutableTreeNode node = new DefaultMutableTreeNode(
									tmp);
							// DefaultMutableTreeNode node = frame.tree
							// .addSubNode(tmp);
							((DefaultMutableTreeNode) child).add(node);
							String secondLayerURL = caculateAbsoluteURL(url,
									matcher.group(1));
							System.out.println(secondLayerURL);
							addLinkAddress(secondLayerURL, node);
						} else {
							DefaultMutableTreeNode node2 = frame.tree
									.addSubNode(tmp);
							frame.tree.addSubNode(nodeZimu, node2);
						}
						((DefaultMutableTreeNode) child).remove(nodeZimu);
						((DefaultMutableTreeNode) child).add(nodeZimu);
					}
				}
			}
			frame.tree.updateUI();
		}
		return secondLayer;
	}

	private static String caculateAbsoluteURL(String source, String part) {
		String[] split = source.split("/");
		String result = "";
		int length = split.length;

		if (part.startsWith("./")) {
			for (int i = 0; i < length - 1; i++) {
				result = result + split[i] + "/";
			}
			result += part.substring(2);
		} else if (part.startsWith("../")) {
			while (part.startsWith("../")) {
				length--;
				part = part.substring(3);
			}
			for (int i = 0; i < length - 1; i++) {
				result = result + split[i] + "/";
			}
			result += part;
		} else if (part.startsWith("http://")) {
			return part;
		} else {
			for (int i = 0; i < length - 1; i++) {
				result = result + split[i] + "/";
			}
			result += part;
		}

		return result;

	}

	public static void addLinkAddress(String url, DefaultMutableTreeNode node) {
		String s = getPageSource(url);
		System.out.println("/////////////////////////////");
		// System.out.print(s);

		/**
		 * <a href=
		 * "ed2k://|file|灵书妙探.Castle.S04E01.Chi_Eng.WEBrip.720X400-YYeTs人人影视.rmv
		 * b | 178952214|a43acb7c5d3b88fcaa405b582258bf19|h=6
		 * adjkdtwknqwuar3m4c2kbhl7a5bvhzn|/" title=
		 * "灵书妙探.Castle.S04E01.Chi_Eng.WEBrip.720X400-YYeTs人人影视.rmvb  170.66 MB"
		 * > 灵书妙探.Castle.S04E01.Chi_Eng.WEBrip.720X400-YYeTs人人影视.rmvb 170.66
		 * MB</a></
		 */

		Pattern pattern = Pattern.compile(
				"<a\\s?href=\"(ed2k.*)\"\\s?title.*>(.*)",
				Pattern.CASE_INSENSITIVE);
		// .compile("<a\\s?href=\"(ed2k.{3,50})\">(.*)$");
		// .compile("<a\\s?href=\"(.{3,})\">.*<font\\scolor=red>" + reg);
		String[] split = s.split("</a>");
		for (int i = 0; i < split.length; i++) {
			Matcher matcher = pattern.matcher(split[i]);
			if (matcher.find()) {
				System.out.println(i + ":");
				String tmp = matcher.group(2);
				System.out.println(tmp);
				String tmp2 = matcher.group(1);
				System.out.println(tmp2);
				DefaultMutableTreeNode c = new DefaultMutableTreeNode(tmp);
				frame.tree.addSubNode(node, c, tmp2);
			}
		}
	}

	public static void test() {

	}
}
